package cn.edu.nuc.onlinestore.frame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import UtilSever.Util;
import cn.edu.nuc.onlinestore.model.Carts;
import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.model.UserCart;

public class UserGoods extends JFrame {

	private JPanel contentPane;
	private JTextField number;
	private JFrame userstore;
	private JFrame userlogin;
	public Goods good;

	
	
	

	public JFrame getUserlogin() {
		return userlogin;
	}


	public void setUserlogin(JFrame userlogin) {
		this.userlogin = userlogin;
	}


	public JFrame getUserstore() {
		return userstore;
	}


	public void setUserstore(JFrame userstore) {
		this.userstore = userstore;
	}




	/**
	 * Launch the application.
	 */
	/*public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				int a = 0;
				try {
					UserGoods frame = new UserGoods(a);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
*/
	/**
	 * Create the frame.
	 */
	public UserGoods(int goodid) {
		
		setTitle("商品详情");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 405);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("商品名称:");
		label.setBounds(23, 57, 66, 15);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("单价:");
		label_1.setBounds(35, 100, 54, 15);
		contentPane.add(label_1);
		
		JLabel label_2 = new JLabel("单位:元");
		label_2.setBounds(321, 100, 54, 15);
		contentPane.add(label_2);
		
		JLabel label_4 = new JLabel("简介:");
		label_4.setBounds(35, 141, 44, 46);
		contentPane.add(label_4);
		
		
		

		//显示商品的详细信息
		good=Util.SeverSelectAGood(goodid);
		
		
		
		
		JButton button = new JButton("加入购物车");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
	
					//得到了所有商品
					Carts car=Util.SeverGetCarts(UserCart.username);
					//判断car是否为空
					if(car==null)
					{
						JOptionPane.showMessageDialog(null,"购物车为空,为您新建购物车好吗？","", JOptionPane.NO_OPTION);
						//新建购物车    添加商品到购物车
						String name = null;
						Map<Goods ,Integer> buy=new HashMap<>();
						car=new Carts(buy,name);
						//捕获输入的商品数量的不是int的异常
						try{
							buy.put(good, Integer.parseInt(number.getText()));
							car.setBuygood(buy);
							car.setUsername(UserCart.username);
							//执行添加购物车
							boolean b=Util.SeverAddCarts(car);
							//判断是否添加成功
							if(b)
							{
								//新建购物车界面
								UserCartFrame cf=new UserCartFrame(car);
								cf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
								//显示购物车界面
								cf.setVisible(true);
								
								//新建store界面
								UserStore st=new UserStore();
								st.setUserLogin(userlogin);
								st.setVisible(true);
								//关闭上一个store窗口
								userstore.dispose();
								//关闭本UserGoodds
								setVisible(false);
							}else{
								JOptionPane.showMessageDialog(null,"添加失败","", JOptionPane.NO_OPTION);
							}
						
						}catch(NumberFormatException e3){
							JOptionPane.showMessageDialog(null,"商品数量错误","", JOptionPane.NO_OPTION);
						}
						
					}else{
						//如果购物车不为空
						//取出map商品列表
						Map<Goods ,Integer> buy=car.getBuygood();
						try{
							buy.put(good, Integer.parseInt(number.getText()));
							car.setBuygood(buy);
							car.setUsername(UserCart.username);
							boolean b=Util.SeverAddCarts(car);
							//判断是否添加成功
							if(b)
							{
								//新建store
								UserStore st=new UserStore();
								st.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
								st.setUserLogin(userlogin);
								st.setVisible(true);
								//关闭上一个store窗口
								userstore.dispose();
								//关闭本UserGoodds
								setVisible(false);
								
								UserCartFrame cf=new UserCartFrame(car);
								//显示购物车
								cf.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
								cf.setVisible(true);
							}else{
								JOptionPane.showMessageDialog(null,"商品库存不足","", JOptionPane.NO_OPTION);
							}
						}catch(NumberFormatException e3){
							JOptionPane.showMessageDialog(null,"商品数量不可以是字符","", JOptionPane.NO_OPTION);
						}
						
					}
	
			}
		});
		
		
		button.setBounds(175, 243, 126, 23);
		contentPane.add(button);
		
		JLabel goodname = new JLabel(good.getName());
		goodname.setBounds(99, 57, 162, 15);
		contentPane.add(goodname);
		
		JLabel price = new JLabel(""+good.getPeice());
		price.setBounds(99, 100, 54, 15);
		contentPane.add(price);
		
		//这里还没写
		JLabel message = new JLabel(""+good.getMessage());
		message.setBounds(99, 141, 251, 46);
		contentPane.add(message);
		
		number = new JTextField();
		number.setText("1");
		number.setBounds(99, 244, 66, 21);
		contentPane.add(number);
		number.setColumns(10);
		
		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setBounds(35, 247, 71, 15);
		contentPane.add(lblNewLabel_2);
		
		JLabel goodnumber = new JLabel(""+good.getNumber());
		goodnumber.setBounds(99, 273, 135, 15);
		contentPane.add(goodnumber);
		
		this.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				((UserGoods)e.getSource()).setVisible(false);
			}
		});
	}
	
	
}
